﻿#include "BinarySearch.h"

int BinarySearch_FindTarget(int target, const int* numbers, int start, int end)
{
    if (end < start) return -1;

    if (target < numbers[start]) return -1;
    if (target > numbers[end]) return -1;

    if (target == numbers[start]) return start;
    if (target == numbers[end]) return end;

    int middle = start + (end - start) / 2;
    if (target == numbers[middle]) return middle;

    if (target < numbers[middle])
    {
        return BinarySearch_FindTarget(target, numbers, start, middle - 1);
    }

    return BinarySearch_FindTarget(target, numbers, middle + 1, end);
}
